﻿using Microsoft.Extensions.Localization;
using PHMEE.Localization;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace PHMEE
{
    public class CompanyManager : DomainService, ICompanyManager
    {
        private readonly IStringLocalizer<PHMEEResource> L;
        private readonly ICompanyRepository companyRepository;

        public CompanyManager(IStringLocalizer<PHMEEResource> L, ICompanyRepository companyRepository)
        {
            this.L = L;
            this.companyRepository = companyRepository;
        }

        public async Task<Company> CreateCompany(string name)
        {
            var existingCompany = await companyRepository.FirstOrDefaultAsync(item => item.Name == name);
            if (existingCompany != null)
            {
                throw new UserFriendlyException(L[PHMEEDomainErrorCodes.CompanyNameAlreadyExist], PHMEEDomainErrorCodes.CompanyNameAlreadyExist).WithData("Name", name);
            }
            var id = GuidGenerator.Create();

            return new Company(id, name);
        }
    }
}
